<?php
require_once(APPPATH.'views/components/page.php');
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<link href='<?php echo base_url() ?>resources/css/admin.css' rel='stylesheet' type='text/css' />
		<script src="<?php echo base_url() ?>resources/js/jquery-1.4.2.min.js"></script>
		<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
		
		<script type="text/javascript">
		    window.onbeforeunload = leavingPageHandler;
		    function leavingPageHandler(e)
		    {
		         var selectall = document.getElementsByName("selectall");
		         selectall[0].checked = false;
		         selectAll(selectall[0]);
		         
		         var ids = document.getElementsByName("ids[]");
		         for (var i = 0, size = ids.length; i < size; i++)
		         {
		         	ids[i].checked = false;
		         }
		         
		         var orders = document.getElementsByName("orders[]");
		         for (var i = 0, size = orders.length; i < size; i++)
		         {
		         	orders[i].checked = false;
		         }
		         
		         var e = document.getElementsByName('categoryview');
				 e[0].selectedIndex = 0;
		    }
		
			function selectAll(selectall)
			{
				var checkboxes = document.getElementsByName("producttypes[]");
				for (var i = 0; i < checkboxes.length; i++)
				{
					checkboxes[i].checked = selectall.checked;
				}
				
				showDeleteBtn(selectall.checked);
			}
			
			function showDeleteBtn(show)
			{
				var deleteBtn = document.getElementById("deletebtn");
				deleteBtn.disabled = !show;
			}
			
			function singleCbClicked()
			{
				var checkboxes = document.getElementsByName("producttypes[]");
				var count = 0;
				for (var i = 0; i < checkboxes.length; i++)
				{
					if (checkboxes[i].checked)
						count++;
				}
				showDeleteBtn(count > 0);
				
				var selectall = document.getElementsByName('selectall');
				selectall[0].checked = (count == checkboxes.length);
					
			}
			
			function onViewBtnClicked(baseurl)
			{
				var e = document.getElementsByName('categoryview');
				var id = e[0].options[e[0].selectedIndex].value;
				
				window.location = baseurl + 'producttype/viewbycategory/' + id;
			}
			
			function onDeleteBtnClicked()
			{
				var checkboxes = document.getElementsByName("producttypes[]");
				var count = 0;
				
				for (var i = 0, size = checkboxes.length; i < size; i++)
				{
					if (checkboxes[i].checked)
						count++;
				}
				
				if (count == 0)
					return false;
					
				return confirm("Xóa " + count + " Nhóm sản phẩm và các Sản phẩm thuộc Nhóm này?");
			}
			
			function onAddBtnClicked()
			{
				var productTypeName = document.getElementById("producttypename").value;
				productTypeName = productTypeName.trim();
				
				if (productTypeName.length == 0)
				{
					alert("Vui lòng nhập tên Nhóm sản phẩm");
					return false;
				}
				
				return true;
			}
			
			function onCancelBtnClicked()
			{
				var productTypeName = document.getElementById("producttypename");
				productTypeName.value = "";
			}
			
			function onSortBtnClicked(baseurl)
			{
				var e = document.getElementsByName('categoryview');
				var id = e[0].options[e[0].selectedIndex].value;
				
				window.location = baseurl + 'producttype/sortview/' + id;
			}
			
			function onCancelSortBtnClicked(baseurl)
			{
				window.location = baseurl + 'producttype';
			}
			
			$(document).ready(function(){
				$('#producttypetable input.move').click(function() {
				var row = $(this).closest('tr');
				var oldOrder = $(this).siblings('#order').val();
				var newOrder = "";
				
				if ($(this).hasClass('up'))
				{
					var prev = row.prev();
					var order = prev.find('#order');
					
					if (order.length == 0)
						return;
					
					newOrder = order.val();
					prev.before(row);
					order.attr('value', oldOrder);
				}
				else
				{
					var next = row.next();
					var order = next.find('#order');
					newOrder = order.val();
					next.after(row);
					order.attr('value', oldOrder);
				}
				var l = $(this).siblings('#order');
				l.attr('value', newOrder);
				});
			});
		</script>
		
		<title>Quản lý nhóm sản phẩm</title>
	</head>
	
	<body>
		<div id='wrapper'>
			<div id='header'>
				<div id="leftTopPan">
				<a href='<?php echo base_url() ?>'><img src='<?php echo base_url() ?>resources/images/logo.gif' title='Consultant' alt='Consultant' width='160' height='39' border='0' /></a>
				</div>
				<h1></h1>
			</div>
			<div id="middle">
				<div id="container">
					<div id="content">
						<br /><h2 align="center">Quản lý nhóm sản phẩm</h2><br />
					<?php
						// Error handler
						$error = $this->session->userdata('error');
						$this->session->unset_userdata('error');
						$errorText;
						
						switch ($error) {
							case INSERT_ERROR:
								$errorText = sprintf(INSERT_ERROR_TEXT, "Nhóm sản phẩm");
								break;
							case UPDATE_ERROR:
								$errorText = UPDATE_ERROR_TEXT;
								break;
							case DELETE_ERROR:
								$errorText = sprintf(DELETE_ERROR_TEXT, "Nhóm sản phẩm");
								break;
							case EXISTED_ERROR:
								$name = $this->session->userdata('producttypename');
								$this->session->unset_userdata('producttypename');
								$errorText = sprintf(EXISTED_ERROR_TEXT,"Nhóm sản phẩm", $name);
								break;
							default:
								$errorText = "";
								break;
						}
						
					?>
					<br />
					<h3 style="color: red"><?php echo $errorText ?></h3>
					<?php
					
						$userdata = $this->session->userdata('userdata');
						
						if ($userdata != NULL)
						{
							Page::getInstance()->generateUserInfo($userdata);
							
							// Add product type
							// Begin add form
							echo form_open('producttype/insert');
							
							// Open fieldset
							echo form_fieldset("Thêm nhóm sản phẩm");
							
							// Text box
							$productType = array(	'id'		=> 'producttypename',
													'name'		=> 'producttypename');
							
							?>
							<p>Tên: <?php echo form_input($productType) ?></p>
							
							<?php
							
							// Options field
							$categoryArr;
							foreach ($categories as $category)
							{
								$categoryArr[$category->getId()] = $category->getTitle();
							}
							
							$selected = count($categories) > 0 ? $categories[0]->getId() : 0;
							?>
							<p>Danh mục: <?php echo form_dropdown('category', $categoryArr, $selected, 'style="width:300px"') ?></p>
							<?php
							
							
							// Add button
							echo "<br/>";
							$addBtn = array(	'id'	=> 'addbtn',
												'value'	=> 'Thêm',
												'onclick'	=> 'return onAddBtnClicked()');
							echo form_submit($addBtn);
							
							// Cancel button
							$cancelBtn = array(	'id'	=> 'resetbtn',
												'value'	=> 'Hủy',
												'onclick'	=> 'onCancelBtnClicked()');
							echo form_reset($cancelBtn);
							
							// Close fieldset
							echo form_fieldset_close();
							
							// End add form
							echo form_close();
							
							//if (count($producttypes) > 0)
							{	
					?>
						<br /><br />
					<?php
								// Open fieldset
								echo form_fieldset("Xem Nhóm sản phẩm theo Danh mục: ");
								
								$sort = $this->session->userdata('sort');
								$this->session->unset_userdata('sort');
								
								// Begin form
								$controller = $sort ? 'producttype/savesorting' : 'producttype/delete';
								echo form_open($controller);
								
								// Category list
								$selected = $this->session->userdata('categoryid');
								$this->session->unset_userdata('categoryid');
								$categoryArr;
								echo form_dropdown('categoryview', $categoryArr, $selected, 'style="width:300px"');
								
								$viewBtn = array(	'id'		=>	'viewbtn',
													'name'		=>	'viewbtn',
													'content'	=>	'Xem',
													'onclick'	=> 	"onViewBtnClicked('".base_url()."')");
								echo form_button($viewBtn);
								
								echo "<br/><br/>";
								
								
								if ($sort == TRUE)
								{
									// Save button
									$saveBtn = array('id'	=>	'savebtn',
													'value'	=>	'Lưu');
									echo form_submit($saveBtn);
									
									$cancelSortBtn = array('id'		=> 'cancelsortbtn',
															'name'	=> 'cancelsortbtn',
															'value'	=>	'Hủy',
															'onclick'=> "onCancelSortBtnClicked('".base_url()."')");
									echo form_reset($cancelSortBtn);
								}
								else
								{
									// Delete button
									$deleteBtn = array('id' => 'deletebtn',
													'disabled' 	=> TRUE,
													'value'		=> 'Xóa',
													'onclick'	=> "return onDeleteBtnClicked()");
									echo form_submit($deleteBtn);
								}
								
								// Sort button
								if (! $sort)
								{
									$sortBtn = array(	'id'	=> 'sortbtn',
														'name'	=> 'sortbtn',
														'content'	=> 'Sắp xếp',
														'onclick'=> "onSortBtnClicked('".base_url()."')");
									echo form_button($sortBtn);
								}
								
								// Product type list
								$js = 'onclick="selectAll(this)"';
					?>
						<table border="1" id="producttypetable">
							<tr>
								<?php if (! $sort): ?>
								<td width="2%"><?php echo form_checkbox('selectall', '', FALSE, $js) ?></td>
								<?php endif ?>
								<th width="60%">Tên</th>
								<?php if (! $sort): ?>
								<th width="20%">Sản phẩm</th>
								<?php endif ?>
								<th width="10%">Thứ tự</th>
							</tr>
					<?php
								$js_show = "onclick='singleCbClicked()'";
								for ($i = 0, $size = count($producttypes); $i < $size; $i++)
								{
					?>
							<tr>
								<?php if (! $sort): ?>
								<td><?php echo form_checkbox('producttypes[]', $producttypes[$i]->getId(), FALSE, $js_show) ?></td>
								<?php endif ?>
								<td><?php echo anchor('producttype/edit/'.$producttypes[$i]->getId(), $producttypes[$i]->getTitle()) ?></td>
								<?php if(! $sort): ?>
								<td><?php echo anchor('producttype/view/'. $producttypes[$i]->getId(), 'Danh sách sản phẩm') ?></td>
								<?php endif ?>
								<td>
									<?php
									if ($sort)
									{
									?>
									<input type="button" value="up" class="move up" />
									<input type="button" value="down" class="move down" />
									<input type="checkbox" id="producttypeid" name="ids[]" checked="checked" style="visibility: hidden" value="<?php echo $producttypes[$i]->getId() ?>"/>
									<input type="checkbox" id="order" name="orders[]" checked="checked" style="visibility: hidden" value="<?php echo $i+1 ?>"/>
									<?php
									}
									else {
									?>
									<label><?php echo $i+1 ?></label>
									<?php
									}
									?>
									
								</td>
							</tr>
					<?php
								}
					?>
						</table>
					<?php
								// End delete form
								echo form_close();
								
								// Close fieldset
								echo form_fieldset_close();
							}
						}
					?>
					</div>
				</div>
					<?php
						if (Page::getInstance()->isAdmin())
						{
							Page::getInstance()->generateAdminLeftMenu();
						}
					?>
			</div>
		</div>
		
	</body>
</html>